{extend name="sitehome@style/base"/}
{block name="resources"}
<style>
    .layui-tab .layui-tab-title li {
        border: 1px solid #e5e5e5;
        border-right: none;
        height: 36px;
        line-height: 36px;
    }

    .shop-title {
        color: #000;
    }

    .red-bar {
        width: 100%;
        height: 2px;
        background: #e8e8e8;
        margin-top: 15px;
        margin-bottom: 8px;
    }

    .reply {
        margin-left: 10px;
    }
</style>
{/block}
{block name="main"}
<div class="nc-table-box">
    <table id="commentList" lay-filter="comment" class="layui-table"></table>
</div>

<script type="text/html" id="comment">
    <div>
        <div class="shop-title">{{d.member_array.title}}</div>
        <div class="red-bar"></div>
        <!-- <span>{{d.member_array.title}}</span> -->
        <div class="reply">
            <div>{{ d.member_array.username}}：{{ d.content}}</div>
            {{# if(d.comment_child){ }}
            {{#  layui.each(d.comment_child, function(index, item){ }}
            <span>{{ item.member_array.username}}回复{{ d.member_array.username}}：{{ item.content}}</span><br>
            {{#  }); }}
            {{# } }}
        </div>
    </div>
</script>

<script type="text/html" id="username">
    <span>{{ d.member_array.username }}</span>
</script>

<!--评论时间-->
<script type="text/html" id="create_time">
    <span>{{nc.time_to_date(d.create_time)}}</span>
</script>


<script type="text/html" id="operation">
    <div>
        {{# if(d.is_show == 1){ }}
        <a class="default" lay-event="isshow">隐藏</a>
        {{# }else{ }}
        <a class="default" lay-event="isshow">显示</a>
        {{# } }}
        <a class="default" lay-event="del">删除</a>
    </div>
    <a class="default" lay-event="reply">回复</a>
</script>

{/block}
{block name="script"}
<script type="text/javascript">
    var table = new Table({
        elem: '#commentList',
        filter: "comment",
        url: nc.url("sns://sitehome/manage/commentlist", {}),
        page: {
            layout: ['count', 'limit', 'prev', 'page', 'next'],
            limit: 10,
            curr: 1
        },
        cols: [
            [{
                    type: 'checkbox',
                    unresize: 'true'
                },
                {
                    toolbar: '#comment',
                    width: '42%',
                    title: '评论',
                    align: 'left',
                    templet: function (data) {
                        return '<a href="#"></a>';
                    }
                }, {

                    width: '15%',
                    title: '评价人',
                    toolbar: '#username',
                    align: 'center',
                    unresize: 'true'
                },
                {
                    field: 'create_time',
                    width: '20%',
                    title: '评价时间',
                    toolbar: '#create_time',
                    align: 'center',
                    unresize: 'true'
                }, {
                    title: '操作',
                    width: '18%',
                    toolbar: '#operation',
                    align: 'center',
                    // unresize: 'true'
                }
            ]
        ]
    });

    //监听工具条
    table.tool(function (obj) {
        var comment_id = obj.data.comment_id;
        console.log(22)
        switch (obj.event) {
            case "isshow":
                var is_show;
                if (obj.data.is_show == 1) {
                    is_show = 0;
                } else {
                    is_show = 1;
                }
                isShowComment(comment_id, is_show);
                break;
            case "del":
                delComment(comment_id, 1);
                break;
            case "reply":
                console.log(obj.data);
                var username = obj.data.member_array.username
                replyComment(comment_id, username);
                break;
        }
    });

    // 隐藏评论
    function isShowComment(comment_id, is_show) {
        $.ajax({
            url: nc.url('sns://sitehome/manage/isShowComment'),
            data: {
                'is_show': is_show,
                'comment_id': comment_id
            },
            type: "GET",
            success: function (res) {
                var page_index = $('.layui-laypage-em').next().html();
                table.reload('commentList', {
                    where: {},
                    page: {
                        curr: page_index
                    }
                })
            }
        })
    }
    // 删除评论
    function delComment(comment_id, is_delete) {
        layer.confirm('确定删除吗?', {
            btn: ['确定', '取消']
        }, function () {
            layer.closeAll('dialog');
            $.ajax({
                url: nc.url('sns://sitehome/manage/delComment'),
                data: {
                    'is_delete': is_delete,
                    'comment_id': comment_id
                },
                type: "GET",
                success: function (res) {
                    var page_index = $('.layui-laypage-em').next().html();
                    table.reload('commentList', {
                        where: {},
                        page: {
                            curr: page_index
                        }
                    })
                }
            })
        });
    }
    // 回复评论
    function replyComment(comment_id, username) {
        var comment_id = comment_id;
        var username = username;
        layer.prompt({
            formType: 2,
            value: '',
            title: '回复 ' + username +':',
            area: ['300px', '200px'] //自定义文本域宽高
        }, function (value, index, elem) {
            $.ajax({
                url: nc.url('sns://sitehome/manage/adminReplyComment'),
                data: {
                    'comment_id': comment_id,
                    'content': value
                },
                type: "POST",
                success: function (res) {
                    var page_index = $('.layui-laypage-em').next().html();
                    table.reload('commentList', {
                        where: {},
                        page: {
                            curr: page_index
                        }
                    })
                }
            })
            layer.close(index);
        })
    }
</script>
{/block}